package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbComboMapper;
import com.bwie.mapper.TbCouponMapper;
import com.bwie.mapper.TbShopCouponMapper;
import com.bwie.pojo.TbCombo;
import com.bwie.pojo.TbCoupon;
import com.bwie.pojo.TbShop;
import com.bwie.pojo.TbShopCoupon;
import com.bwie.service.TbShopService;
import com.bwie.mapper.TbShopMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Lenovo
* @description 针对表【tb_shop】的数据库操作Service实现
* @createDate 2025-07-15 13:15:42
*/
@Service
public class TbShopServiceImpl extends ServiceImpl<TbShopMapper, TbShop>
    implements TbShopService{

    @Autowired
    TbShopMapper tbShopMapper;

    @Autowired
    TbCouponMapper tbCouponMapper;

    @Autowired
    TbComboMapper tbComboMapper;

    @Autowired
    TbShopCouponMapper tbShopCouponMapper;

    @Override
    public R getComboList(PageVo pageVo) {
        Page<TbCombo> tbComboPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        QueryWrapper<TbCombo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pageVo.getName())) {
            queryWrapper.lambda().like(TbCombo::getComboName,pageVo.getName());
        }
        Page<TbCombo> comboPage = tbComboMapper.selectPage(tbComboPage, queryWrapper);
        return R.OK(comboPage);
    }

    @Override
    public R getShopList(PageVo pageVo) {
        Page<TbShop> tbShopPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        QueryWrapper<TbShop> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pageVo.getName())) {
            queryWrapper.lambda().like(TbShop::getShopName,pageVo.getName());
        }
        Page<TbShop> shopPage = tbShopMapper.selectPage(tbShopPage, queryWrapper);
        return R.OK(shopPage);
    }

    @Override
    public R getCouponList(PageVo pageVo) {
        Page<TbCoupon> tbCouponPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        QueryWrapper<TbCoupon> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pageVo.getName())) {
            queryWrapper.lambda().like(TbCoupon::getCouponName,pageVo.getName());
        }
        Page<TbCoupon> couponPage = tbCouponMapper.selectPage(tbCouponPage, queryWrapper);
        return R.OK(couponPage);
    }

    @Override
    public R buyCombo(TbCombo tbCombo) {
        //抢购套餐，并修改状态
        tbCombo.setComboStatus(1);
        tbComboMapper.updateById(tbCombo);
        return R.OK();
    }

    @Override
    public R cancleCoupon(TbShop tbShop) {
        //核销优惠券，并修改状态
        tbShop.setShopStatus(2);
        tbShopMapper.updateById(tbShop);
        return R.OK();
    }

    @Override
    public R concatCoupon(TbShop tbShop) {
        //关联优惠券 多对多
//        for (Integer couponId : tbShop.getCouponList()) {
//            TbShopCoupon tbShopCoupon = new TbShopCoupon();
//            tbShopCoupon.setShopId(tbShop.getShopId());
//            tbShopCoupon.setCouponId(couponId);
//            tbShopCouponMapper.insert(tbShopCoupon);
//        }
        for (TbCoupon tbCoupon : tbShop.getCouponList()) {
            TbShopCoupon tbShopCoupon = new TbShopCoupon();
        tbShopCoupon.setShopId(tbShop.getShopId());
           tbShopCoupon.setCouponId(tbCoupon.getCouponId());
          tbShopCouponMapper.insert(tbShopCoupon);
        }

        return R.OK();
    }
}




